package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.CommunityShopDao;
import cn.hs.ph.entity.CommunityShop;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommunityShopDaoImpl implements CommunityShopDao {
    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        String sql = "select shop.*,communityshop.id from communityshop join shop on shop.shopId=communityshop.shopId JOIN community on community.communityId=communityshop.communityId WHERE community.communityId=? ";
        List<Object> params = new ArrayList<>();
        params.add(map.get("communityId"));
        String shopName = map.get("shopName");
        if(shopName!=null && !"".equals(shopName)){
            sql+=" and shop.shopName like ?";
            params.add("%"+shopName+"%");
        }
        String num = map.get("pageNum");
        if(num!=null && !"".equals(num)){
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(num);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum-1)*pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                Shop shop = new Shop(
                        res.getInt(1),
                        res.getString(2),
                        res.getString(3),
                        res.getString(4),
                        res.getString(5),
                        res.getString(6),
                        res.getString(7),
                        res.getString(8),
                        res.getDate(9),
                        res.getString(10),
                        res.getString(11),
                        res.getInt(12),
                        res.getInt(13),
                        res.getInt(14),
                        res.getInt(15),
                        res.getString(16),
                        res.getString(17),
                        res.getString(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getString(22),
                        res.getString(23),
                        res.getString(24)
                );
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int total=0;
        List<Shop> list = new ArrayList<>();
        String sql = "select count(1) from communityshop join shop on shop.shopId=communityshop.shopId JOIN community on community.communityId=communityshop.communityId WHERE community.communityId=? ";
        List<Object> params = new ArrayList<>();
        params.add(map.get("communityId"));
        String shopName = map.get("shopName");
        if(shopName!=null && !"".equals(shopName)){
            sql+=" and shop.shopName=?";
            params.add("shopName");
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true){
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return total;
    }

    @Override
    public int delete(int shopId) {
        String sql = "delete from communityshop where shopId = ?";
        Object obj[] = {shopId};
        return JDBCDruidUtil.update(sql,obj);
    }


    @Override
    public int addShop(int shopId,int communityId) {
        String sql = "insert into communityshop values(null,?,?)";
        Object obj[] = {communityId,shopId};
        return JDBCDruidUtil.update(sql,obj);
    }
}
